package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j

public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetMealMapper setMealMapper;


    /**
     * 新增菜品和对应口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional //事务，同成功同失败
    public void saveWithFlavor(DishDTO dishDTO) {
        log.info("新增菜品和对应口味：{}", dishDTO);
        Dish dish = new Dish();
        //拷贝
        BeanUtils.copyProperties(dishDTO, dish);
//        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insert(dish);

        //获得dish表中的主键
        Long id = dish.getId();

        //获取dishflavor中的口味集合
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 1) {
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(id));
            //向口味表添加数据
            dishFlavorMapper.insert(flavors);
        }


    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * 1.启售中的菜品不能被删除
     * 2.套餐中的菜品不能被删除
     * 3.如果删了菜品，菜品口味表中数据也要删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //启售中的菜品不能被删除
        //查询对于id的菜品是否是起售状态
        //select ？ from dish where id in(1,2,3,4) and status=1
        Integer enableCount = dishMapper.getEnableCount(ids, StatusConstant.ENABLE);
        log.info("查询到起售状态菜品：{}", enableCount);
        if (enableCount > 0) {
            //启售的菜品不能被删除
            log.error("启售的菜品不能被删除");
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //2.套餐中的菜品不能被删除
        Integer setMealCount = setMealMapper.getSetMeal(ids);
        if (setMealCount > 0) {
            //套餐中的菜品不能被删除
            log.error("套餐中的菜品不能被删除");
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.如果删了菜品，菜品口味表中数据也要删除
        //删除dish表
        dishMapper.deleteById(ids);
        //删除dish_flavor表关联数据
        dishFlavorMapper.deleteById(ids);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */

    public DishVO list(Long id) {
        log.info("根据id查询菜品：{}", id);
        //根据id查询dish表数据
        Dish dish = dishMapper.getById(id);
        //根据id查询dishFlavor表数据
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);

        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 根据id修改菜品以及口味
     *
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //根据id修改dish表中数据
        //拷贝dto数据到dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);
        //根据id删除口味表原有数据
        Long id = dishDTO.getId();
        dishFlavorMapper.deleteByIdOne(id);

        //重新插入新的数据--新增操作
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 1) {
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     */
    @Override
    public void startOrClose(Integer status, Long id) {
        log.info("菜品起售、停售：{}，{}",status,id);
        dishMapper.startOrClose(status,id);
    }



    /**
     * 根据分类id查询菜品
     * @param
     * @param categoryId
     * @param name
     * @return
     */
    public List<Dish> seletByCategoryId(Long categoryId, String name) {
        log.info("根据分类id查询菜品:{}",categoryId);
        List<Dish> list=dishMapper.getByCategoryId(categoryId,name);
        return list;
    }
}

